package com.example.userlogin.controller;


import com.example.userlogin.entity.Employee;
import com.example.userlogin.entity.Purchase;
import com.example.userlogin.service.EmployeeService;
import com.example.userlogin.service.PurchaseService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/purchase")
//专门处理供应商控制器
public class PurchaseController {
    //    实现与前端交互的数据请求方法
//    1.进入模块首页的时候需要对数据里的所有商品数据进行查询，返回列表给前端页面进行展示
    @Autowired
    PurchaseService purchaseService;

    @Autowired
    HttpServletRequest request;

    //    1.展示数据库里已有的商品列表给前端
    @RequestMapping("/purchaseList")
    public String PurchaseMain(Model model){
//        调用service方法查询到商品列表结果
        List<Purchase> purchaseList = purchaseService.findAll();
        Object supplier = request.getSession().getAttribute("supplier");
//        将查询结果用model绑定
        model.addAttribute("data",purchaseList);
        model.addAttribute("supplier",supplier);
        System.out.println(purchaseList);
//        返回视图
        return "purchase/purchaseList";
    }

    @RequestMapping("/purchaseAdd")
    public String purchaseAdd(Model model){
        Object supplier = request.getSession().getAttribute("supplier");
        model.addAttribute("supplier",supplier);
        return "purchase/purchaseAdd";
    }

    @RequestMapping("/purchaseAddProcess")
    public String purchaseAddProcess(@RequestParam String supplier, @RequestParam String name,
                                     @RequestParam String category, @RequestParam Integer number,
                                     @RequestParam double price,@RequestParam String time,
                                     @RequestParam double amount
    ){
        //      组装一个goods实体对象
        Purchase  purchase = new Purchase();
        purchase.setSupplier(supplier);
        purchase.setName(name);
        purchase.setCategory(category);
        purchase.setNumber(number);
        purchase.setPrice(Double.valueOf(price));
        purchase.setTime(time);
        purchase.setAmount(Double.valueOf(amount));
        //        调用service的add方法给数据库
        if (purchaseService.add(purchase)==1){
            //        如果返回ok，那么就进入商品管理首页
            return "redirect:/purchase/purchaseList";
        }else {
            return "redirect:/purchase/purchaseList";
        }
    }

    @RequestMapping("/purchaseUpdate")
    public String update(@RequestParam String id,Model model){
//        首先基于id查询出来该id记录里的所有数据信息
        Purchase purchase=new Purchase();
        purchase.setId(Integer.valueOf(id));
//        调用service方法查询出来该记录的数据信息
        Purchase purchaseDB = purchaseService.findOne(purchase);
//        2.将这些数据信息返回给视图
        model.addAttribute("data",purchaseDB);
        Object name = request.getSession().getAttribute("name");
        model.addAttribute("name",name);
//        3.返回视图文件
        return "purchase/purchaseUpdate";
    }

    @RequestMapping("/purchaseUpdateProcess")
    public String purchaseUpdateProcess(@RequestParam String supplier, @RequestParam String name,
                                        @RequestParam String category, @RequestParam Integer number,
                                        @RequestParam double price,@RequestParam String time,
                                        @RequestParam double amount
    ){
        //      组装一个goods实体对象
        Purchase  purchase = new Purchase();
        purchase.setSupplier(supplier);
        purchase.setName(name);
        purchase.setCategory(category);
        purchase.setNumber(Integer.valueOf(number));
        purchase.setPrice(Double.valueOf(price));
        purchase.setTime(time);
        purchase.setAmount(Double.valueOf(amount));
        //        调用service的add方法给数据库
        if (purchaseService.update(purchase)==1){
            //        如果返回ok，那么就进入商品管理首页
            return "redirect:/purchase";
        }else {
            return "redirect:/purchase";
        }
    }

}
